﻿using System;
using System.Net.Mail;
using System.Configuration;

namespace TempEmployees.BLL.Notifications
{
    public class EMailService
    {
        private MailMessage message;
        private SmtpClient smtpClient;

        private static readonly string m_fromAddress;
        private static readonly string m_fromDisplayName;
        private static readonly string m_debugAddress;

        static EMailService()
        {
            EmailConfiguration configuration = ConfigurationManager.GetSection("EmailService") as EmailConfiguration;
            if (configuration == null) return;
            m_fromAddress = configuration.FromAddress;
            m_fromDisplayName = configuration.FromDisplayName;
            m_debugAddress = configuration.DebugAddress;
        }

        public EMailService()
        {
            message = new MailMessage();
            smtpClient = new SmtpClient();

            // displayes as the sender
            SetFrom(m_fromAddress, m_fromDisplayName);
            // send to debug
            AddBcc(m_debugAddress);
        }

        private void SetFrom(string address)
        {
            message.From = new MailAddress(address);
        }

        public void SetFrom(string address, string displayName)
        {
            message.From = new MailAddress(address, displayName);
        }

        public void AddTo(params string[] addresses)
        {
            foreach (string address in addresses)
            {
                message.To.Add(address);
            }
        }

        public void AddCC(params string[] addresses)
        {
            foreach (string address in addresses)
            {
                message.CC.Add(address);
            }
        }

        public void AddBcc(string address)
        {
            message.Bcc.Add(address);
        }

        public void SetSubject(string subject)
        {
            message.Subject = subject;
        }

        public void SetBody(string body, bool isBodyHtml)
        {
            message.Body = body;
            message.IsBodyHtml = isBodyHtml;
        }

        public void SetPriority(MailPriority priority)
        {
            message.Priority = priority;
        }

        public void AddAttachment(string filename)
        {
            message.Attachments.Add(new Attachment(filename));
        }

        public void SetReplyTo(string address)
        {
            message.ReplyToList.Add(new MailAddress(address));
        }

        public void SendMail()
        {
            if (message.From == null) message.From = new MailAddress(m_fromAddress, m_fromDisplayName);

            try
            {
                smtpClient.Send(message);
            }
            catch (SmtpFailedRecipientsException smtpRecipientsEx)
            {
                // TODO
                throw smtpRecipientsEx;
            }
            catch (SmtpFailedRecipientException smtpRecipientEx)
            {
                // TODO
                throw smtpRecipientEx;
            }
            catch (SmtpException smtpEx)
            {
                // TODO
                throw smtpEx;
            }
        }

        public static string TestSendMail()
        {
            EMailService mailer = new EMailService();
            //mailer.AddTo("ohad_lights@hotmail.com", "ylauner@gmail.com");
            mailer.SetSubject("testing mailer from temporary employees website");
            mailer.SetBody("<h1>Hi,</h1>this is a testing mail!", true);
            try
            {
                mailer.SendMail();
                return "Mail sent successfully?";
            }
            catch (Exception ex)
            {
                string error = "exception message: " + ex.Message;
                if (ex.InnerException != null) error = error + " | inner message: " + ex.InnerException.Message;
                return error;
            }
        }
    }
}
